﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Model.Result.QixingManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-09-17 22:30
    /// 描 述：服务类
    /// </summary>
    public class StoreOrderMasterService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<StoreOrderMasterEntity>> GetList(StoreOrderMasterListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<StoreOrderMasterEntity>> GetPageList(StoreOrderMasterListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<StoreOrderMasterEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<StoreOrderMasterEntity>(id);
        }

        public StoreOrderDto GetGoodsOrderEntity(long id)
        {
            if (id.IsNullOrZero())
            {
                return null;
            }
            StringBuilder strSql = new StringBuilder();
            var parameter = new List<DbParameter>();
            strSql.Append(@"SELECT a.Id,a.order_sn,a.shipping_user,a.ship_mobile,SUM(b.goods_cnt) as product_cnt,
                            a.create_time,a.order_money,
                            a.remark FROM storeOrderMaster  a  
                            JOIN storeOrderDetail b ON a.Id=b.order_id
                             ");
            strSql.Append(@" where a.Id=@order_id  GROUP BY a.Id,a.order_sn,a.shipping_user,a.ship_mobile,
                            a.create_time, a.order_money,
                            a.remark");
            parameter.Add(DbParameterExtension.CreateDbParameter("@order_id", id));
            return this.BaseRepository().FindList<StoreOrderDto>(strSql.ToString(), parameter.ToArray()).Result.FirstOrDefault();
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(StoreOrderMasterEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                StoreOrderMasterEntity masterEntity=this.BaseRepository().FindEntity<StoreOrderMasterEntity>(entity.Id.Value).Result;
                masterEntity.pay_money = entity.pay_money;
                await this.BaseRepository().Update(masterEntity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<StoreOrderMasterEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<StoreOrderMasterEntity, bool>> ListFilter(StoreOrderMasterListParam param)
        {
            var expression = LinqExtensions.True<StoreOrderMasterEntity>();
            if (param != null)
            {
            }
            return expression;
        }
        #endregion
    }
}
